package com.luyue.zagj;

import android.Manifest;
import android.annotation.TargetApi;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.luyue.zagj.adapter.MyPagerAdapter;
import com.luyue.zagj.fragment.HomeFragment;
import com.luyue.zagj.fragment.MyAccountFragment;
import com.luyue.zagj.fragment.ServiceZFragment;
import com.luyue.zagj.view.MyViewPager;
import com.umeng.analytics.MobclickAgent;
import java.util.ArrayList;

import static com.zhy.autolayout.utils.ScreenUtils.getStatusBarHeight;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private MyViewPager main_viewpager;
    private ArrayList<Fragment> fragmentList = new ArrayList<>();
    private MyPagerAdapter mMyPagerAdapter;
    private final int SDK_PERMISSION_REQUEST = 127;
    private String permissionInfo;
    private int i = 0;
    private LinearLayout mainBottomeSwitcherContainer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //initStateBar();
        setContentView(R.layout.activity_main);
        initView();
        setListener();
        initViewpager();
        getPersimmions();
    }


    private void initView() {
        mainBottomeSwitcherContainer = (LinearLayout) findViewById(R.id.main_bottome_switcher_container);
        main_viewpager = (MyViewPager) findViewById(R.id.main_viewpager);
    }

    private void setListener() {
        int childCount = mainBottomeSwitcherContainer.getChildCount();
        //遍历控件中所有孩子的数量
        for (int i = 0; i < childCount; i++) {
            FrameLayout frameLayout = (FrameLayout) mainBottomeSwitcherContainer.getChildAt(i);
            frameLayout.setOnClickListener(this);
        }
    }

    private void initViewpager() {
        fragmentList.clear();
        fragmentList.add(new HomeFragment());
        fragmentList.add(new ServiceZFragment());
        fragmentList.add(new MyAccountFragment());

        mMyPagerAdapter = new MyPagerAdapter(this.getSupportFragmentManager(), fragmentList);
        main_viewpager.setAdapter(mMyPagerAdapter);
        main_viewpager.setPagingEnabled(false);
         onClick(mainBottomeSwitcherContainer.getChildAt(0));

    }
    @Override
    public void onClick(View v) {
            changeUI(mainBottomeSwitcherContainer.indexOfChild(v));
            changeFragment(mainBottomeSwitcherContainer.indexOfChild(v));

    }

    /**
     * 改变底部栏状态
     * @param postion
     */
    private void changeUI(int postion) {
        int childCount = mainBottomeSwitcherContainer.getChildCount();
        for (int i = 0; i < childCount; i++) {
            if (i == postion) {
                setEnable(mainBottomeSwitcherContainer.getChildAt(i), false);
            } else {
                setEnable(mainBottomeSwitcherContainer.getChildAt(i), true);
            }
        }
    }

    private void setEnable(View itemView, boolean enable) {
        itemView.setEnabled(enable);
        if (itemView instanceof ViewGroup) {
            int childCount = ((ViewGroup) itemView).getChildCount();
            for (int i = 0; i < childCount; i++) {
                setEnable(((ViewGroup) itemView).getChildAt(i), enable);
            }
        }
    }
    /**改变fragemnt状态
     * @param postion
     */
    private void changeFragment(int postion) {
        main_viewpager.setCurrentItem(postion);
    }
    @TargetApi(23)
    private void getPersimmions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            ArrayList<String> permissions = new ArrayList<String>();
            /***
             * 定位权限为必须权限，用户如果禁止，则每次进入都会申请
             */
            // 定位精确位置
            if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
            }
            if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
            }
            /*
			 * 读写权限和电话状态权限非必要权限(建议授予)只会申请一次，用户同意或者禁止，只会弹一次
			 */
            // 读写权限
            if (addPermission(permissions, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                permissionInfo += "Manifest.permission.WRITE_EXTERNAL_STORAGE Deny \n";
            }
            // 读取电话状态权限
            if (addPermission(permissions, Manifest.permission.READ_PHONE_STATE)) {
                permissionInfo += "Manifest.permission.READ_PHONE_STATE Deny \n";
            }

            if (permissions.size() > 0) {
                requestPermissions(permissions.toArray(new String[permissions.size()]), SDK_PERMISSION_REQUEST);
            }
        }
    }
    @TargetApi(23)
    private boolean addPermission(ArrayList<String> permissionsList, String permission) {
        if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) { // 如果应用没有获得对应权限,则添加到列表中,准备批量申请
            if (shouldShowRequestPermissionRationale(permission)) {
                return true;
            } else {
                permissionsList.add(permission);
                return false;
            }
        } else {
            return true;
        }
    }
    //触发系统弹窗   提示获取权限 用户选择后触发Activity中的回调函数：
    @TargetApi(23)
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        // TODO Auto-generated method stub
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
    @Override
    public void onBackPressed() {
        if (i == 0) {
            i = i + 1;
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
        } else {
            finish();
        }
    }


    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
